﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.Entity;
using FastORM.Applicaiton;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("CodeItem")]
    public class CodeItemController : FastTemplateController<Frame_CodeItem>
    {
        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Code/CodeItem_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/Code/CodeItem_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Code/CodeItem_Edit.cshtml");
        }


        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string MainGuid = Convert.ToString(Params["MainGuid"]);
            string ItemText = Convert.ToString(Params["ItemText"]);
            if (!string.IsNullOrEmpty(MainGuid))
            {
                ListWhereText += string.Format(" and MainGuid='{0}'", MainGuid);
            }
            if (!string.IsNullOrEmpty(ItemText))
            {
                ListWhereText += string.Format(" and ItemText like '%{0}%'", ItemText);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_CodeItem Entity)
        {
            ReturnValue result = new ReturnValue();
            int ExistCount = Convert.ToInt32(DBUtil.ExecuteScar(string.Format("select count(1) from Frame_CodeItem where MainGuid='{0}' and  ItemValue='{1}'", Entity.MainGuid, Entity.ItemValue)));
            if (ExistCount > 0)
            {
                result.Add("code", -100);
                result.Add("msg", "已存在相同值的具体代码项！");
                return result;
            }
            else
            {
                return base.OperateAdd(Entity);
            }

        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }
    }
}
